package com.xiaomili.halfsugar.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.bean.HomeBean;

import java.util.List;

/**
 * Created by WangQi on 2016/7/15.
 */
public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<Object> mList;
    private static final int TYPE_NROMAL = 0;
    private static final int TYPE_DOUBLE = 1;
    private static final int TYPE_BIG = 2;
    private int mIndex;
    private List<Integer> mListIndex;
    private HomeBean.DataBean.InsertElementBean.ElementListBean mBean;

    public HomeRecyclerViewAdapter(Context context, int index, List<Integer> listIndex, List<Object> list) {
        this.mContext = context;
        this.mList = list;
        this.mIndex = index;
        this.mListIndex = listIndex;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_DOUBLE) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_insert_item1, parent, false);
            return new DoublePicHolder(view);
        }
        if (viewType == TYPE_BIG) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_insert_item2, parent, false);
            return new BigPicHolder(view);
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_home_item, parent, false);
        return new NormalViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        //一般类型
        if (getItemViewType(position) == TYPE_NROMAL) {
            Log.i("bbb", "onBindViewHolder: " + position);
            HomeBean.DataBean.TopicBean bean = (HomeBean.DataBean.TopicBean) mList.get(position);
            String type = bean.type;
            final NormalViewHolder normalViewHolder = (NormalViewHolder) holder;

            //标题
            normalViewHolder.tv_title.setText(bean.title);

            //评论
            String comments = bean.comments;
            if (comments.equals("")) {
                normalViewHolder.tv_Comments.setText("0");
            } else {
                normalViewHolder.tv_Comments.setText(comments);
            }

            //点赞
            if (type.equals("")) {
                normalViewHolder.iv_isLike.setImageResource(R.mipmap.ic_topic_list_item_multi_likes);
                String likes = bean.likes;
                if (likes.equals("")) {
                    normalViewHolder.tv_Likes.setText("0");
                } else {
                    normalViewHolder.tv_Likes.setText(bean.likes);
                }
            }

            //浏览
            if (type.equals("2")) {
                normalViewHolder.iv_isLike.setImageResource(R.mipmap.ic_topic_list_item_multi_views);
                String views = bean.views;
                if (views.equals("")) {
                    normalViewHolder.tv_Likes.setText("0");
                } else {
                    normalViewHolder.tv_Likes.setText(bean.views);
                }
            }

            // 图片
            Picasso.with(mContext)
                    .load(bean.pic)
                    .into(normalViewHolder.iv_pic);
            normalViewHolder.tv_Nicename.setText(bean.user.nickname);

            normalViewHolder.normal_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(position, normalViewHolder.normal_layout);
                    }
                }
            });
        }

        //双图类型
        if (getItemViewType(position) == TYPE_DOUBLE) {
            final DoublePicHolder doublePicHolder = (DoublePicHolder) holder;
            final HomeBean.DataBean.InsertElementBean.ElementListBean insertElementBean = (HomeBean.DataBean.InsertElementBean.ElementListBean) mList.get(position);
            Picasso.with(mContext).load(insertElementBean.data.get(0).photo).into(doublePicHolder.iv1);
            Picasso.with(mContext).load(insertElementBean.data.get(1).photo).into(doublePicHolder.iv2);
            doublePicHolder.iv1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(position, doublePicHolder.iv1);
                    }
                }
            });
            doublePicHolder.iv2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(position, doublePicHolder.iv2);
                }
            });
        }

        //大图类型
        if (getItemViewType(position) == TYPE_BIG) {
            BigPicHolder bigPicHolder = (BigPicHolder) holder;
            HomeBean.DataBean.InsertElementBean.ElementListBean insertElementBean = (HomeBean.DataBean.InsertElementBean.ElementListBean) mList.get(position);
            Picasso.with(mContext).load(insertElementBean.data.get(0).photo).into(bigPicHolder.iv);
        }
    }

    @Override
    public int getItemViewType(int position) {
        for (int i = 0; i < mListIndex.size(); i++) {
            int num = mListIndex.get(i);
            mBean = (HomeBean.DataBean.InsertElementBean.ElementListBean) mList.get(num);
            if (position == num) {
                if (mBean.data.size() == 2) {
                    return TYPE_DOUBLE;
                } else if (mBean.data.size() == 1) {
                    return TYPE_BIG;
                } else {
                    return TYPE_NROMAL;
                }
            }
        }
        return TYPE_NROMAL;
    }

    @Override
    public int getItemCount() {
        Log.i("aaa", "getItemCount: " + mList.size());
        return mList.size();
    }


    class NormalViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv_pic;
        public TextView tv_title;
        public TextView tv_Nicename;
        public ImageView iv_isLike;
        public TextView tv_Likes;
        public TextView tv_Comments;//评论
        public LinearLayout normal_layout;

        public NormalViewHolder(View itemView) {
            super(itemView);
            iv_pic = (ImageView) itemView.findViewById(R.id.pic_iv);
            tv_title = (TextView) itemView.findViewById(R.id.title_tv);
            tv_Nicename = (TextView) itemView.findViewById(R.id.nickname_tv);
            iv_isLike = (ImageView) itemView.findViewById(R.id.islike_iv);
            tv_Likes = (TextView) itemView.findViewById(R.id.likes_tv);
            tv_Comments = (TextView) itemView.findViewById(R.id.comments_tv);
            normal_layout = (LinearLayout) itemView.findViewById(R.id.normal_layout);
        }
    }

    class DoublePicHolder extends RecyclerView.ViewHolder {
        public ImageView iv1, iv2;

        public DoublePicHolder(View itemView) {
            super(itemView);
            iv1 = (ImageView) itemView.findViewById(R.id.insert_item_iv1);
            iv2 = (ImageView) itemView.findViewById(R.id.insert_item_iv2);
        }
    }

    class BigPicHolder extends RecyclerView.ViewHolder {
        public ImageView iv;

        public BigPicHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(int position, View itemView);
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }
}
